package com.efounder.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.CountDownTimer;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;

import com.efounder.core.VoicePlayer;
import com.efounder.mobilecomps.R;


/**
 * Created by meijun on 17-3-8.
 */

public class PlayView extends ImageView {

    Bitmap copeBitmap  = null;
    private Bitmap bitmap;
    private Bitmap bitmap1;
    private int mH;
    private int mW;
    private String TAG = "iMAGEVIEW";
    private Paint paint;
    private RectF oval;
    float weep = 90;
    public PlayView(Context context) {
        super(context);
        init();
    }

    private void init() {
        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.blue_checked);
        //bitmap1 = ((BitmapDrawable) getResources().getDrawable(R.mipmap.blue_checked)).getBitmap();
        // bitmap.createBitmap(copeBitmap,0,0, bitmap.getWidth(), bitmap.getHeight());
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStrokeWidth(4);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(Color.BLUE);
        oval = new RectF();
    }

    public PlayView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public PlayView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        this.mW = w;
        this.mH = h;
        Matrix matrix = new Matrix();
        // 缩放原图
        matrix.postScale( mW * 1.0f / bitmap.getWidth() ,mH * 1.0f /  bitmap.getHeight());
        //  bitmapsrc  bitmapsrc beginx  bitmapsrc beginy  src width src height ,matrix
        bitmap1 = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
        super.onSizeChanged(w, h, oldw, oldh);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
      canvas.drawBitmap(bitmap1,0,0,null);
        oval.set(0+20,0+20,mW-20,mH-20);
        canvas.drawArc(oval,-90,weep,false,paint);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {


        this.setMeasuredDimension(dp2px(100.f),dp2px(100.f));
    }




    public int  dp2px(float value){
        return (int) (this.getContext().getResources().getDisplayMetrics().density * value + 0.5f);
    }

    /**
     * Main Thread;
     * @param percent 0-1
     */
    public void setWeep(float percent){

        float v = percent *  360 ;
        weep = v;
        invalidate();
    }

    public void start(final int count) {
        Log.e("ddd", "onTidddddddddck: " );

        CountDownTimer countDownTimer = new CountDownTimer(count*1000, 10) {
            @Override
            public void onTick(long millisUntilFinished) {

                float v = millisUntilFinished * 1.0f /(count * 1000) ;
                Log.e(TAG, "onTick: "+v );
                setWeep(1 - v);
            }

            @Override
            public void onFinish() {
                if(mListener!=null){
                    mListener.playComplete();
                }
            }
        };
        countDownTimer.start();

    }

    private VoicePlayer.OnstateChangeListener mListener;
    public void setonStateChangeListener(VoicePlayer.OnstateChangeListener listener){
        this.mListener = listener;
    }
    public 	interface  OnstateChangeListener {

        /**
         * 播放完成
         */
        void runFinish();


    }


    @Override
    public void setVisibility(int visibility) {
        super.setVisibility(visibility);
            setWeep(0);


    }
}
